//
//  JBOAuthViewController.m
//  OCsinaBlog
//
//  Created by jobo on 16/4/27.
//  Copyright © 2016年 jobo. All rights reserved.
//

#import "JBOAuthViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "JBAccount.h"
#import "MJExtension.h"
#import "JBTabBarController.h"
@interface JBOAuthViewController () <UIWebViewDelegate>

@end

@implementation JBOAuthViewController


//#define AppKey 2218523684 //appkey
//#define AppSecret 818b6fd28223cc8213a4a645d25509b1 //AppSecret
//authorize https:api.weibo.com/oauth2/authorize //authorize

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSString *AppKey = @"2172418954";
    NSString *redirect_uri = @"https://m.baidu.com/" ;
    
    UIWebView *wbV = [[UIWebView alloc] init];
    wbV.frame = self.view.bounds;
    wbV.delegate = self ;
    
    [self.view addSubview:wbV];
    
    
    NSString *URL = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",AppKey,redirect_uri];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URL]];
    
    [wbV loadRequest:request];
    
    

}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *code = [request.URL.query componentsSeparatedByString:@"="][1];
    
    [self getOAuthAccessTokenWithCode:code];
    
    
    
    return YES ;
}

- (void)getOAuthAccessTokenWithCode : (NSString *)code
{
    NSString *access_token_url = @"https://api.weibo.com/oauth2/access_token" ;
    NSString *grant_type = @"authorization_code";
    NSString *AppKey = @"2172418954";
    NSString *AppSecret = @"173661ce2b78da7a7cd2dd35b9b7ca8c" ;
    NSString *redirect_uri = @"https://m.baidu.com/" ;

    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    params[@"client_id"] = AppKey ;
    params[@"client_secret"] = AppSecret ;
    params[@"grant_type"] = grant_type ;
    params[@"code"] = code ;
    params[@"redirect_uri"] = redirect_uri ;
    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager] ;
    //设置afnetW的数据解析方式为2进制,默认是json
    manager.responseSerializer = [AFHTTPResponseSerializer serializer] ;
    
    [manager POST:access_token_url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //获取返回的数据
        //NSString *response = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        //利用MJextension 将返回来的二进制数据解析保存到模型
        JBAccount *account = [JBAccount objectWithJSONData: responseObject];
        //
        if(account.access_token != nil)
        {
            [account saveDataToDefault];
            JBTabBarController *tabarRootViewController = [[JBTabBarController alloc] init] ;
            [UIApplication sharedApplication].keyWindow.rootViewController = tabarRootViewController ;
        }
        else
        {
            NSLog(@"登录失败") ;
        }
        
        
        NSLog(@"response =====%@",account.access_token) ;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //NSLog(@"error ====== %@",error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
